package com.silas.dynflightdatatransfer;

import java.time.ZoneOffset;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.silas.dynflightdatatransfer.dao.FlightDynamicDataInfoMapper;
import com.silas.dynflightdatatransfer.model.entity.FlightDynamicDataInfo;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;

@Order(value = 2)
@Component
public class DoTransfer implements CommandLineRunner {

    @Autowired
    private FlightDynamicDataInfoMapper flightDataMapper;

    @Override
    public void run(String... args) {
        System.out.println("start transfer ...");

        getFlightDataInBatch();
    }

    private void getFlightDataInBatch() {
        DateTime startDate = DateUtil.parseDate("2024-01-01");
        Long startEpoch = startDate.toLocalDateTime().toInstant(ZoneOffset.of("+8")).toEpochMilli();
        Long endEpoch = startDate.toLocalDateTime().plusHours(1).toInstant(ZoneOffset.of("+8")).toEpochMilli();

        LambdaQueryWrapper<FlightDynamicDataInfo> queryWrapper = new LambdaQueryWrapper<FlightDynamicDataInfo>()
            .ge(FlightDynamicDataInfo::getTimeStamp, startEpoch)
            .le(FlightDynamicDataInfo::getTimeStamp, endEpoch);

        List<FlightDynamicDataInfo> flightDataList = flightDataMapper.selectList(queryWrapper);

        System.out.printf("List size: %d\n", flightDataList.size());

        for (FlightDynamicDataInfo flightDynamicDataInfo : flightDataList) {
            int i = 10;

            if ( i-- == 0 ) {
                break;
            }

            System.out.println(flightDynamicDataInfo.getTimeStamp());
        }
    }
}
